<?php
class setPage{
	public $pageWord = array('First','Last',' The ',' Page ',' Total ',' ',' record ');
	//var $pageWord = array('第一页','最后页','第','页','总共','条','记录');
	public $menuFirst;
	public $menuEnd;
	public $menuAll;
	public $allCount;
	public $allPage;
	public $pageURL;
	public $pageURLNoAnd;
	public $pageNow;
	public $limitStart;
	public $limitNum;
	public $limit;
	public $onePageLink;
	public $linkMiddle;
	
	function setPagePHP($onePageRecord,$onePageLink,$allCount,$pageRoot='',$type=true){
		global $dbconn;
		$this->limitNum = $onePageRecord;
		$this->onePageLink = $onePageLink;
		if (!$pageRoot){
			$pageRoot = end(explode('/',$_SERVER['PHP_SELF']));
		}
		if (!is_integer($allCount)){
			//echo $allCount;
			$this->allCount = $dbconn->Getonefield($allCount);
		}else {
			$this->allCount = $allCount;
		}
		$this->allPage = ceil($this->allCount/$this->limitNum);
		if ($_GET[pageid] < 1){
			$this->pageNow = 1;
		}elseif ($_GET[pageid] > $this->allPage){
			$this->pageNow = $this->allPage;
		}else {
			$this->pageNow = $_GET[pageid];
		}
		if ($type){ /////如果要生成动态地址
			$URLQuery = '';
			if(is_array($_GET)&&$_GET){
				foreach($_GET as $key=>$val){
					if ($key!='pageid'){////delete the var of nowpage member;
						if($URLQuery == ""){
							$URLQuery = "?$key=$val";
						}else{
							$URLQuery .= "&$key=$val";
						}
					}
				}
			}
			$URLQuery .= $URLQuery?'&':'?';
		}
		$this->pageURL = $pageRoot.$URLQuery;
		$this->pageURLNoAnd = substr($this->pageURL,0,-1);
		if ($type){
			$this->setPageOne();
		}else {
			$this->setPageTwo();
		}
	}
	function setPageOne(){
		if ($this->allPage > $this->onePageLink){
			$pageHalf = floor($this->onePageLink/2);
			if ($this->pageNow <= $pageHalf + 1){
				$pageFirst = 1;	
			}elseif ($this->pageNow >= $this->allPage - $pageHalf){
				$pageFirst = $this->allPage - $this->onePageLink + 1;
			}else {
				$pageFirst = $this->pageNow - $pageHalf;
			}
			$istart = $pageFirst;
			$iend = $pageFirst+$this->onePageLink;
		}else {
			$istart = 1;
			$iend = $this->allPage + 1;
		}
		for ($i = $istart; $i < $iend; $i++){
			if ($i == $this->pageNow){
				$this->linkMiddle .= "<span class='type1'><strong>".$i."</strong></span>";
			}else {
				if ($i==1){
					$this->linkMiddle .= "<span class='type3'><a href='".$this->pageURLNoAnd."'>1</a></span>";
				}else {
					$this->linkMiddle .= "<span class='type3'><a href='".$this->pageURL.'pageid='.$i."'>".$i."</a></span>";
				}
			}
		}
		$this->limitStart = ($this->pageNow-1)*$this->limitNum;
		$this->limit = " limit ".$this->limitStart.",".$this->limitNum;
		$this->menuFirst = "<span class='type2'><a href='".$this->pageURLNoAnd."'>".$this->pageWord[0]."</a></span>";
		$this->menuEnd = "<span class='type2'><a href='".$this->pageURL.'pageid='.$this->allPage."'>".$this->pageWord[1]."</a></span>";
		$this->menuAll = "<span class='type4'><span>".$this->pageWord[4].'<b>'.$this->allPage.'</b>'.$this->pageWord[3].'</span></span>'.$this->menuFirst.$this->linkMiddle.$this->menuEnd;
	}
	function setPageTwo(){
		
	}
}

?>